package com.example.mybatis.demo.mapper;

import com.example.mybatis.demo.model.UserInfo;
import org.apache.ibatis.annotations.*;


import java.util.List;

@Mapper
public interface UserInfoMapper {
//    @Select("select id ,username, password ,age, gender, phone," +
//            "delete_flag as deleteFlag,create_time as createTime,update_time as updateTime from user_info")

    @Results(id="resultMap",value = {
            @Result(column = "delete_flag",property = "deleteFlag"),
            @Result(column = "create_time",property = "createTime"),
            @Result(column = "update_time",property = "updateTime"),
    })
    @Select("select id ,username, password ,age, gender, phone from user_info")
    List<UserInfo> selectAll();

    //尽可能避免使用*
   // @Select("select * from user_info where id=#{id}")
    @ResultMap(value = "resultMap")
    @Select("select id ,username, password ,age, gender, phone from user_info where id=#{id}")
    UserInfo selectIDALL(Integer id);

    //非唯一主键查询，返回结果建议使用List
    //@Select("select * from user_info where age=#{age} and gender=#{gender}")
    //@Select("select * from user_info where age=#{param1} and gender=#{param2}")与顺序强绑定不推荐
    @Select("select * from user_info where age=#{age1} and gender=#{gender2}")
    List<UserInfo> selectAgeAndGender(@Param("age1") Integer age, @Param("gender2") Integer gender);

    //传递对象
    @Insert("insert into user_info(username,`password`,age,gender)" +
            "values (#{username},#{password},#{age},#{gender})")
    Integer insertUser(UserInfo userInfo);

    @Insert("insert into user_info(username,`password`,age,gender)" +
            "values (#{userinfo.username},#{userinfo.password},#{userinfo.age},#{userinfo.gender})")
    Integer insert(@Param("userinfo") UserInfo userInfo);

    //如果想要拿到自增id
    @Options(useGeneratedKeys = true,keyProperty = "id")
    @Insert("insert into user_info(username,`password`,age,gender)" +
            "values (#{userinfo.username},#{userinfo.password},#{userinfo.age},#{userinfo.gender})")
    Integer insert1(@Param("userinfo") UserInfo userInfo);

    //删除数据
    @Delete("delete from user_info where id=#{id}")
    void delete(Integer id);

    //改数据
    @Update("update user_info set username=#{username} where id =#{id}")
    void updata (String username,Integer id);
    //创对象来改数据
    @Update("update user_info set username=#{username} where id =#{id}")
    Integer updata1 (UserInfo userInfo);

    //sql注入案例
    @Select("select username,`password`,age,gender,phone from user_info where username='${username}' and password='${password}' ")
    List<UserInfo> queryUserByPassword(String username,String password);

    //排序功能
    @Select("select * from user_info order by id ${order}")
    //用#查询，String默认会加上单引号，所以只能用$查询
    List<UserInfo> selectUesrByOrder(String order);

    //模糊查询
    @Select("select * from user_info where username like concat('%',#{name},'%')")
    List<UserInfo> selectUesrByName(String name);



}
